﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Lon.Server
{
    public partial class LogViewForm : Form
    {

        LogViewType logViewType = LogViewType.Info;
        public LogViewType LogType
        {
            get { return logViewType; }
            set { logViewType = value; }
        }
        
        public LogViewForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            FillTreeView();
            if (logViewType == LogViewType.Info)
            {
                this.Text = "系统日志";
            }
            else
            {
                this.Text = "异常日志";
            }

            base.OnLoad(e);
        }

        string GetLogPath()
        {
            string logPath = "Log\\Info";

            if (logViewType == LogViewType.Error)
            {
                logPath = "Log\\Error";
            }

            return Path.Combine(Application.StartupPath, logPath);

        }

        void FillTreeView()
        {
            string path = GetLogPath();
            string[] fileList = Directory.GetFiles(path, "*.htm", SearchOption.TopDirectoryOnly);
            
            Array.Sort(fileList);

            for (int i = fileList.Length - 1; i >= 0; i-- )
            {
                string fileName = fileList[i];
                string text = Path.GetFileNameWithoutExtension(fileName);
                TreeNode node = treeView1.Nodes.Add(text);
                node.ImageIndex = 0;
                node.Tag = fileName;
            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
                
            string fileName = e.Node.Tag.ToString();
            statusLabel1.Text = fileName;

            //MessageBox.Show(e.Node.Tag.ToString());
            webBrowser1.Url = new Uri(@"file:///" + fileName);

        }

    }

    public enum LogViewType
    {
        Info,
        Error,
    }

}
